package com.py.korona.ui.activities.category;

import java.util.HashMap;
import java.util.LinkedHashMap;
import android.app.Activity;
import android.app.Fragment;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import com.py.korona.R;
import com.py.korona.adapters.CustomListAdapterMapping;
import com.py.korona.adapters.CustomListAdapterWrapper;
import com.py.korona.db.UriBuilder;
import com.py.korona.db.model.CategoriesInRoomEntity;
import com.py.korona.db.model.CategoryEntity;
import com.py.korona.db.model.RoomEntity;
import com.py.korona.ui.activities.room.RoomDetailActivity;
import com.py.korona.ui.activities.room.RoomListBinder;

public class CategoryDetailFragmentViewer extends Fragment {
	private static final int ACTIVITY_EDIT = 1;
	private CategoryDetailFragmentListener listener;
	private HashMap<String, Bitmap> imageCache = new LinkedHashMap<String, Bitmap>();
	private String _id;

	public CategoryDetailFragmentListener getListener() {
		return listener;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		/*
		 * 
		 * room list for category
		 */
		CustomListAdapterWrapper wrapper = new CustomListAdapterWrapper();
		wrapper.setMapping(new CustomListAdapterMapping() {
			@Override
			public Uri contentURI() {
				return UriBuilder.objectListUri(
						"RoomsWithinCategoryContentProvider",
						"roomsWithinCategory");
			}

			@Override
			public String[] contentProperties() {
				return new String[] { RoomEntity.TableFieldAliasID(),
						RoomEntity.TableFieldAliasText() };
			}

			@Override
			public ViewBinder binder() {
				return new RoomListBinder(imageCache);
			}

			@Override
			public int[] bindToLayoutViews() {
				return new int[] { R.id.ListCellRoomEditName };
			}

			@Override
			public int bindToLayout() {
				return R.layout.cell_room_list_within_category;
			}

			@Override
			public String[] bindFromColumns() {
				return new String[] { RoomEntity.FieldNameText };
			}

			@Override
			public String filter() {
				return String.format("%s=?",
						CategoriesInRoomEntity.TableFieldAliasCategory());
			}

			@Override
			public String[] filterArgs() {
				return new String[] { _id };
			}
		});
		loadData();
		wrapper.setContext(getActivity());
		getLoaderManager().initLoader(0, null, wrapper);
		/*
		 * init list
		 */
		OnItemClickListener onItemClickListener = new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				Cursor cursor = (Cursor) adapterView
						.getItemAtPosition(position);
				if (cursor.moveToFirst()) {
					ContentValues cv = new ContentValues();
					cv.put(RoomEntity.FieldNameID, id);
					Uri uri = UriBuilder.objectFilterUri(
							RoomEntity.uriAuthority(),
							RoomEntity.uriBasePath(), cv);
					String contentType = UriBuilder.contentType(RoomEntity
							.uriBasePath());
					Intent intent = new Intent(getActivity(),
							RoomDetailActivity.class);
					intent.putExtra("readonly", true);
					intent.putExtra(contentType, uri);
					startActivityForResult(intent, ACTIVITY_EDIT);
				}
			}
		};
		ListView listView = (ListView) getActivity().findViewById(
				R.id.listView1);
		listView.setAdapter(wrapper.adapter());
		listView.setScrollingCacheEnabled(false);
		listView.setOnItemClickListener(onItemClickListener);
		this.registerForContextMenu(listView);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.activity_category_detail_view, menu);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(
				R.layout.activity_category_detail_fragment_view, null);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		boolean result = false;
		switch (item.getItemId()) {
		case android.R.id.home: {
			listener.fragmentOnCloseAction(this);
			result = true;
			break;
		}
		case R.id.DetailsMenuItemDelete: {
			listener.fragmentOnDeleteAction(this);
			listener.fragmentOnCloseAction(this);
			break;
		}
		case R.id.DetailsMenuItemEdit: {
			listener.fragmentOnEditAction(this);
			break;
		}
		default: {
			result = super.onOptionsItemSelected(item);
			break;
		}
		}
		return result;
	}

	public void setListener(CategoryDetailFragmentListener listener) {
		this.listener = listener;
	}

	private void loadData() {
		TextView mTitleText = (TextView) getActivity().findViewById(
				R.id.categoryPreviewName);
		ContentValues contentValues = listener.fragmentContentValues(this);
		String text = contentValues.getAsString(CategoryEntity.FieldNameText);
		mTitleText.setText(text);
		// FIXME: remove reading of id
		this._id = contentValues.getAsString(CategoryEntity.FieldNameID);
	}
}
